/**
 * 
 */
package com.byzk.p2p.admin.user.controller;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.byzk.p2p.admin.bank.dto.Bank;
import com.byzk.p2p.admin.bank.service.BankCardService;
import com.byzk.p2p.admin.base.controller.AdminBaseController;
import com.byzk.p2p.admin.base.util.JsonMsg;
import com.byzk.p2p.admin.base.util.PageInfo;
import com.byzk.p2p.admin.city.dto.City;
import com.byzk.p2p.admin.city.service.CityService;
import com.byzk.p2p.admin.user.dto.UserInfoDto;
import com.byzk.p2p.admin.user.service.UserInfoService;

/*** <p>Title:UserInfoController </p>
* <p>Description:用户信息管理 </p>
* <p>Company:xbd </p>
* @author:Administrator
* @date:2016年10月18日下午5:30:53 */
@Controller
@RequestMapping(value="/admin/userinfo")
public class UserInfoController extends AdminBaseController {

	@Autowired
	private  UserInfoService userInfoService;
	
	@Autowired
	private  BankCardService bankCardService;
	
	@Autowired
	private  CityService cityService;
	/**
	 * 
	* <p>Description:用户信息列表 </p>
	* <p>Company:xbd </p>
	* @author:lilei
	 * @throws UnsupportedEncodingException 
	* @date:2016年10月19日上午10:24:14
	 */
	@RequestMapping(value = "/list")
	public String userInfoList(Model model,
			@RequestParam(value = "index", required = false, defaultValue = "1") Integer index,
			@RequestParam(value = "limit", required = false, defaultValue = "10") Integer limit,
			@RequestParam(value = "username", required = false) String userName,
			@RequestParam(value = "realname", required = false) String realName,
			@RequestParam(value = "phone", required = false) String phone,
			@RequestParam(value = "sendTimeFrom", required = false) String sendTimeFrom,
			@RequestParam(value = "sendTimeTo", required = false) String sendTimeTo) throws UnsupportedEncodingException {
		Map<String, Object> queryMap = new HashMap<String, Object>();
		if (userName != null && !StringUtils.isBlank(userName)) {
			userName=java.net.URLDecoder.decode(userName , "UTF-8");
			queryMap.put("userName", userName);
			model.addAttribute("userName", userName);
		}
		if (realName != null && !StringUtils.isBlank(realName)) {
			realName=java.net.URLDecoder.decode(realName , "UTF-8");
			queryMap.put("realName", realName);
			model.addAttribute("realName", realName);
		}
		if (phone != null && !StringUtils.isBlank(phone)) {
			queryMap.put("phone", phone);
			model.addAttribute("phone", phone);
		}
		if (sendTimeFrom != null && !StringUtils.isBlank(sendTimeFrom)) {
			queryMap.put("sendTimeFrom", sendTimeFrom);
			model.addAttribute("sendTimeFrom", sendTimeFrom);
		}
		if (sendTimeTo != null && !StringUtils.isBlank(sendTimeTo)) {
			queryMap.put("sendTimeTo", sendTimeTo);
			model.addAttribute("sendTimeTo", sendTimeTo);
		}
		PageInfo<UserInfoDto> pageInfo = userInfoService.userInfoList(queryMap, index, limit);
		model.addAttribute("pageInfo", pageInfo);
		return ftl(Thread.currentThread().getStackTrace());
	}
   
	/**
	* <p>Description：根据用户编号查询用户信息 </p>
	* <p>Company:xbd </p>
	* @author:lilei
	* @date:2016年10月19日下午12:36:13
	 */
	@RequestMapping(value = "/querylist",method=RequestMethod.GET)
	public String userInfoListByTerm(Model model,
			@RequestParam(value = "index", required = false, defaultValue = "1") Integer index,
			@RequestParam(value = "limit", required = false, defaultValue = "10") Integer limit,
			@RequestParam(value = "uid" ,required=false) Integer uid,
			@RequestParam(value = "pid",required=false) Integer pid
			) {

		Map <String,Object> reqParam = new HashMap<String,Object>();
		City city = new City();
		reqParam.put("uid", uid);
		PageInfo<UserInfoDto> pageInfo = userInfoService.userInfoList(reqParam, 0, limit);	
		List<Bank> bankList = bankCardService.listBack();
		if(pid!=null){
			city.setPid(pid);
			model.addAttribute("pid", pid);
		}
		List<City> cityList = cityService.getCityEnum(city);
        
		model.addAttribute("bankList", bankList);
		model.addAttribute("item", pageInfo.getPageList().get(0));
		model.addAttribute("cityList", cityList);
		return ftl(Thread.currentThread().getStackTrace());
	}
	
	/**
	 * 
	 * <p>
	 * Description:修改或绑定用户银行卡信息
	 * </p>
	 * <p>
	 * Company:xbd
	 * </p>
	 * 
	 * @author:lilei
	 * @throws Exception
	 * @date:2016年10月20日上午11:14:53
	 */
	@RequestMapping(value = "/modify", method = RequestMethod.POST)
	public void modifyUserInfo(Model model, @RequestBody UserInfoDto dto) throws Exception {
		if (dto.getBankCode() == null || StringUtils.isBlank(dto.getBankCode())) {
			printJSON(new JsonMsg(1, "开户行名称不能为空！"));
			return;
		}
		if (dto.getCityCode() == null || StringUtils.isBlank(dto.getCityCode())) {
			printJSON(new JsonMsg(2, "开户地区不能为空！"));
			return;
		}
		if (dto.getCardNo() == null || StringUtils.isBlank(dto.getCardNo())) {
			printJSON(new JsonMsg(3, "银行卡号不能为空！"));
			return;
		}
		Integer result = userInfoService.updateOrSave(dto);
		if (result > 0) {
			printJSON(new JsonMsg(-1, "修改成功！"));
			return;
		} else {
			printJSON(new JsonMsg(4, "修改失败！"));
			return;
		}
	}
}
